﻿namespace Yom.Common
{
    using System.Configuration;
    using System.Collections.Generic;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;
    using Yom.Common.Model;

    public sealed class SiteLoad
    {
        private static readonly Caches _Cache = new Caches();
        private static readonly Dictionary<string, string> _LanCache = new Dictionary<string, string>();
        private static readonly Dictionary<string, string> _UrlCache = new Dictionary<string, string>();

        private SiteLoad() { }

        public static string GetLan(string sVal)
        {
            if (_LanCache.Count <= 0)
                MntLan();

            string str = string.Empty;

            if (!string.IsNullOrEmpty(sVal))
                str = _LanCache.TryGetValue(sVal, out str) ? str : sVal;

            return str;
        }

        public static string GetUrl(string sVal)
        {
            if (_UrlCache.Count <= 0)
                MntUrl();

            foreach (KeyValuePair<string, string> pair in _UrlCache)
            {
                if (Regex.IsMatch(sVal, pair.Key, RegexOptions.IgnoreCase))
                    return Regex.Replace(sVal, pair.Key, pair.Value, RegexOptions.IgnoreCase);
            }

            return string.Empty;
        }

        private static void MntLan()
        {
            string str = Path.Combine(Factory.RootPath, "Locale");
            string path = Path.Combine(str, Scheme.Locale + ".xml");

            if (!File.Exists(path))
                path = Path.Combine(str, "ZH-CN.xml");

            XmlTextReader reader = new XmlTextReader(path);
            string attribute = string.Empty;

            for (string str2 = string.Empty; reader.Read(); str2 = string.Empty)
            {
                if (reader.Name == "Add")
                {
                    attribute = reader.GetAttribute("Key");
                    str2 = reader.GetAttribute("Value");

                    if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(str2))
                    {
                        _LanCache[attribute] = str2;
                    }
                }
                attribute = string.Empty;
            }
            reader.Close();
        }

        private static void MntUrl()
        {
            XmlTextReader reader = new XmlTextReader(Path.Combine(Factory.RootPath, "Rewrite.xml"));
            string attribute = string.Empty;

            for (string str = string.Empty; reader.Read(); str = string.Empty)
            {
                if (reader.Name == "Add")
                {
                    attribute = reader.GetAttribute("Key");
                    str = reader.GetAttribute("Value");

                    if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute))
                    {
                        attribute = string.Format("{0}", attribute);
                        str = string.Format("{0}", str);
                        _UrlCache[attribute] = str;
                    }
                }
                attribute = string.Empty;
            }
            reader.Close();
        }

        private static Yom.Model.Admin AdminCookieGet()
        {
            Yom.Model.Admin model = new Yom.Model.Admin();
            model.AdminID = 0;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[SiteLoad.Cookie + "_Admin"];

            if (cookie != null)
            {
                string strID = cookie.Values.Get("aID");
                int ID = !string.IsNullOrEmpty(strID) ? strID.ToInt() : 0;

                if (ID > 0)
                {
                    model = new Yom.Data.Admin().GetModel(ID);
                }
            }

            return model;
        }

        private static void AdminCookieSet(Yom.Model.Admin model)
        {
            HttpCookie cookie = new HttpCookie(SiteLoad.Cookie + "_Admin");
            cookie.Domain = SiteLoad.CookieDomain;
            cookie.Values.Add("aID", model.AdminID.ToString());
            HttpContext.Current.Response.Cookies.Set(cookie);
        }

        private static void AdminCookieDel()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[SiteLoad.Cookie + "_Admin"];

            if (cookie != null)
            {
                cookie.Domain = SiteLoad.CookieDomain;
                cookie.Expires = System.DateTime.Now.AddMinutes(-2.0);
                cookie.Values.Clear();
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }

        public static SiteSet Scheme
        {
            get
            {
                object Set = _Cache["SiteSet"];

                if (Set == null)
                {
                    Set = SiteSet.GetSet();
                    _Cache["SiteSet"] = Set;
                }

                return (Set as SiteSet);
            }
        }

        public static Yom.Model.Admin Admin
        {
            get
            {
                Yom.Model.Admin model = AdminCookieGet();
                return model;
            }
            set
            {
                if (value == null)
                    AdminCookieDel();
                else
                    AdminCookieSet(value);
            }
        }

        public static string Code
        {
            get
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[SiteLoad.Cookie + "_Code"];
                string code = string.Empty;
                if (cookie != null)
                    code = cookie.Value;

                return code;
            }
            set
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[SiteLoad.Cookie + "_Code"];

                if (cookie == null)
                    cookie = new HttpCookie(SiteLoad.Cookie + "_Code");

                cookie.Domain = SiteLoad.CookieDomain;
                cookie.Expires = System.DateTime.Now.AddMinutes(10.0);

                if (cookie.Values.Count > 0)
                    cookie.Values.Clear();

                cookie.Values.Add("Code", value);
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }

        public static string Cookie
        {
            get
            {
                return ConfigurationManager.AppSettings["Cookie"].ToString();
            }
        }

        public static string CookieDomain
        {
            get
            {
                return ConfigurationManager.AppSettings["CookieDomain"].ToString();
            }
        }
    }
}